%% Least squares fit using fminsearch

%Create a noisy data set
X=(1:10);
A=2;
B=4;
ExpectedSignal=A*X+B; %Noise free or 'Expected' signal

figure
plot(X,ExpectedSignal)
xlabel('X')
ylabel('Signal')

% The noisy signal - add Gaussian noise. 
Sigma_Noise=10;
NoisySignal=ExpectedSignal+Sigma_Noise*randn(size(ExpectedSignal));

hold on
plot(X,NoisySignal,'ro')
% Note - Noisy data should not be plotted with line.  A line suggests a model. 
% Use a marker.  

% Setup a function to be minimized
lsqlinefit=@(X0,Data,X) sum( (Data-(X0(2)+X0(1)*X)).^2   );

% Setup an intial guess
X0=[1 1]; %[A B]

%Create an 'Options' structure with default parameters. 
OPTIONS = optimset();
%OPTIONS.Display='iter' %show updates

%doc fminsearch

% Have fminsearch adjust parameters to minimize function
ABfound = fminsearch(lsqlinefit,X0,OPTIONS,NoisySignal,X)
% Note all inputs to fminsearch after 'OPTIONS' are send to the function
% being minimized as additional inputs. 

% Plot our result with the data
hold on
FoundModel=ABfound(2)+ABfound(1)*X
plot(X,FoundModel,'g','linewidth',2)
legend('Expected','Noisy Data','Found Model')

s=sprintf('A: %g  B: %g',ABfound(1),ABfound(2))
title(s)

% Try re-running this script and notice the different fit values with each
% different realization of the noise. 




